package com.wk.controller.teacher;

import com.wk.pojo.All_files;
import com.wk.pojo.User;
import com.wk.service.Courseservice;
import com.wk.service.Fileservice;
import com.wk.utils.responseresult;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 教师管理文件
 * @author: 王侃
 * @create: 2020-03-26 19:58
 **/
@Controller
@RequestMapping("/tea")
public class Teafilescontroller {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    Courseservice courseservice;
    @Autowired
    Fileservice fileservice;

    //跳转到发布资料界面
    @RequestMapping("/file/upload")
    public String file_upload()
    {
        return "teacher/files/file_upload";
    }

    //文件发布接收至数据库
    @RequestMapping("/file/uploadsubmit")
    @ResponseBody
    public responseresult file_upload(MultipartFile file, HttpSession session)
    {
        User user=(User)session.getAttribute("loginuser");
        logger.info("============>文件上传");
        //上传至阿里云oss
        //上传图片和视频并且获得路径
        String path=courseservice.upload(file);
        //将所要上传的信息封装成类
        All_files files=new All_files();
        files.setName(file.getOriginalFilename());
        files.setCreatetime(new Timestamp(new Date().getTime()));
        files.setUser_id(user.getId());
        files.setUsername(user.getUsername());
        files.setPath(path);
        files.setClick(0);
        //加入数据库
        fileservice.insertintoAll_files(files);
        return responseresult.success();
    }

    //跳转到资料管理界面
    @RequestMapping("/file/management")
    public String file_management(HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<All_files> files=fileservice.selectbyuser_id_files(user.getId());
        model.addAttribute("files",files);
        return "teacher/files/management";
    }

    //删除自己的资料
    @RequestMapping("/file/delete")
    public String file_delete(Integer id)
    {
        fileservice.deletebymyfile(id);
        return "common/success";
    }
}
